import * as themeMap from "juejin-markdown-themes/dist"
import {ThemeListItem} from "bytemd-plugin-theme/dist/types/types";

export const themeList: ThemeListItem[]  = Object.keys(themeMap).filter(key => key !== "default").map( key => ({
    title: key,
    style: "",
    rewrite: "",
    theme: key,
    highlight: themeMap[key]?.highlight,
}))
export async function changeTheme(theme: ThemeListItem | string): Promise<void> {
    let str = ""
    if(typeof theme === 'string') {
        str = theme
    }else{
        const {title, style } = theme;
        let themeNames;
        if(themeNames.includes(title)) {
            str = themeMap[title]?.style
        }else{
            str = style
        }
    }

    let markdownThemeStyleElement = document.querySelector('#markdownTheme')
    if (markdownThemeStyleElement) {
        markdownThemeStyleElement.innerHTML = str
    } else {
        markdownThemeStyleElement = document.createElement('style')
        markdownThemeStyleElement.id = 'markdownTheme'
        markdownThemeStyleElement.innerHTML = str
        document.head.appendChild(markdownThemeStyleElement)
    }
}


